if [ -z $1 ]
then
    echo "usage: ./sd_fusing.sh <SD Reader's device file>"
    exit 0
fi

if [ -b $1 ]
then
    echo "$1 reader is identified."
else
    echo "$1 is NOT identified."
    exit 0
fi

if [ -z $2 ] #判断参数2的字符串是否为空，如果为空，则打印出帮助信息
then
	echo "usage: sd_fusing.sh <SD Reader's device file> <Source file>"
	exit 0
fi

####################################
#<verify device>

BDEV_NAME=`basename $1`
BDEV_SIZE=`cat /sys/block/${BDEV_NAME}/size`

if [ ${BDEV_SIZE} -le 0 ]; then
	echo "Error: NO media found in card reader."
	exit 1
fi

if [ ${BDEV_SIZE} -gt 64000000 ]; then
	echo "Error: Block device size (${BDEV_SIZE}) is too large"
	exit 1
fi

TARGET_FILE=./target.bin

cat E4412.S.BL1.SSCR.EVT1.1.bin E4412.BL2.TZ.SSCR.EVT1.1.bin all00_padding.bin $2 E4412.TZ.SSCR.EVT1.1.bin > $TARGET_FILE

#<SD fusing>
echo "---------------------------------------"
echo "SD fusing"
dd iflag=dsync oflag=dsync if=$TARGET_FILE of=$1 seek=1
rm $TARGET_FILE
# 如果失败则退出
if [ $? -ne 0 ]
then
	echo Write TARGET_FILE Error!
	exit -1
fi
#<flush to disk>
sync

####################################
#<Message Display>
echo "---------------------------------------"
echo "source image is fused successfully."
echo "Eject SD card and insert it again."

